<?php
/*
 * 企业库编辑
 * @package    company
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: bfac4026a36b3dd93013cbb4a23a543d810b6620 $
 */

defined('IN_IXDCW') or exit('Access Denied');
require DT_CORE . '/' . $module . '/common.inc.php';
require_once DT_INC . '/post.func.php';
User::checkLogin();
$id = isset($id) ? intval($id) : 0;
if (empty($id)) Prompt::message();



/* 创建词条 */
if ($createSubmit) {
	if($com_year)	if(!is_numeric($com_year) ) Prompt::message('年份填写异常','goback');
	if($com_money)	if(!is_numeric($com_money)) Prompt::message('注册资金填写异常','goback');
	if($com_type == '-') $com_type = '';
	if($com_size == '-') $com_size = '';

	//验证
	if(empty($cates))           Prompt::message('类别填写异常！','goback');
	if(empty($com_areaid))      Prompt::message('地区填写异常！','goback');
	if(!is_numeric($com_size))  Prompt::message('公司规模填写异常！','goback');
	if($com_money<0)            Prompt::message('注册资金填写异常！','goback');
	if(is_int($com_year) && strlen($com_year)<4){
		Prompt::message('注册年份填写异常！','goback');
	}
	if(!empty($com_telephone)){
		if(!is_mobile($com_telephone)) Prompt::message('电话号码填写异常！','goback');
	}

	//主营范围
	$com_business = CompanyEntry::getBusiness($cates);

	$sql = Sql::update(Table::name(Table::NAME_MERCHANT_DOC), array(
		'title'           => $title,
		'summary'         => $summary,
		'company_thumb'   => $com_thumb,
		'company_type'    => $com_type,
		'company_size'    => $com_size,
		'company_year'    => $com_year,
		'company_unit'    => $com_unit,
		'company_money'   => $com_money,
		'company_areaid'  => $com_areaid,
		'company_address' => $com_address,
		'company_contact' => $com_contact,
		'company_telephone'=> $com_telephone,
		'company_mail'    => $com_mail,
		'company_business'=> $com_business,

		'reason'          => $reason,
		'authorid'        => $_userid,
		'author'          => $_username,
		'time'            => time(),
	),'did='.$id);

	$did = $id;
	if (!$db->query($sql)) 	exit($db->error());



	$sql = Sql::insert(Table::name(Table::NAME_MERCHANT_EDITION), array(
		'did'             => $did,
		'content'         => $content,
		'summary'         => $summary,
		'company_thumb'   => $com_thumb,
		'company_type'    => $com_type,
		'company_size'    => $com_size,
		'company_year'    => $com_year,
		'company_unit'    => $com_unit,
		'company_money'   => $com_money,
		'company_areaid'  => $com_areaid,
		'company_address' => $com_address,
		'company_contact' => $com_contact,
		'company_telephone'=> $com_telephone,
		'company_mail'    => $com_mail,
		'company_business'=> $com_business,

		'reason'          => $reason,
		'author'          => $_username,
		'authorid'        => $_userid,
		'time'            => time(),
	));

	if (!$db->query($sql)) 	exit($db->error());
	$eid = $db->insertId();
	$did = $id;

	//添加类别
	CompanyEntry::addCategory($cates,$did,$eid,$_username,$_userid);

	//更新词条的eid
	$table = Table::name(Table::NAME_MERCHANT_DOC);
	$sql = "update $table set eid=$eid where did=$did";
	if (!$db->query($sql))  exit($db->error());

	//设置 类别 主要版本
	$sql = Sql::update(Table::name(Table::NAME_MERCHANT_CATID), array('authority' => 1 ), 'did='.$did.' and eid='.$eid);
	if (!$db->query($sql))  exit($db->error());

	//删除草稿
	CompanyEntry::deleteDraft($did,$_userid);

	if($db->query($sql)) Prompt::message('创建成功, 请等待管理员审核！','list.php');

/* 编辑词条 */
} elseif ($editSubmit) {
	if($com_year)	if(!is_numeric($com_year) ) Prompt::message('年份填写异常','goback');
	if($com_money)	if(!is_numeric($com_money)) Prompt::message('注册资金填写异常','goback');
	if($com_type == '-') $com_type = '';
	if($com_size == '-') $com_size = '';

	//验证
	if(empty($cates))           Prompt::message('类别填写异常！','goback');
	if(empty($com_areaid))      Prompt::message('地区填写异常！','goback');
	if(!is_numeric($com_size))  Prompt::message('公司规模填写异常！','goback');
	if($com_money<0)            Prompt::message('注册资金填写异常！','goback');
	if(is_int($com_year) && strlen($com_year)<4) Prompt::message('注册年份填写异常！','goback');
	if(!empty($com_telephone)){
		if(!is_mobile($com_telephone)) Prompt::message('电话号码填写异常！','goback');
	}


	//主营范围
	$com_business = CompanyEntry::getBusiness($cates);


	$sql = Sql::insert(Table::name(Table::NAME_MERCHANT_EDITION), array(
			'did'             => $id,
			'content'         => $content,
			'summary'         => $summary,
			'company_thumb'   => $com_thumb,
			'company_type'    => $com_type,
			'company_size'    => $com_size,
			'company_year'    => $com_year,
			'company_unit'    => $com_unit,
			'company_money'   => $com_money,
			'company_areaid'  => $com_areaid,
			'company_address' => $com_address,
			'company_contact' => $com_contact,
			'company_telephone'=> $com_telephone,
			'company_mail'    => $com_mail,
			'company_business'=> $com_business,

			'reason'          => $reason,
			'author'          => $_username,
			'authorid'        => $_userid,
			'time'            => time(),
	));


	if (!$db->query($sql)) {
		echo $db->error();
		exit('Error');
	}
	$did = $id;
	$eid = $db->insertId();

	//添加类别
	CompanyEntry::addCategory($cates,$did,$eid,$_username,$_userid);

	//删除草稿
	CompanyEntry::deleteDraft($did,$_userid);

	Prompt::message('编辑成功, 请等待管理员审核！','list.php');


//后台编辑处理
} elseif ($editAdminSubmit) {

	if($com_year)	if(!is_numeric($com_year) ) Prompt::message('年份填写异常','goback');
	if($com_money)	if(!is_numeric($com_money)) Prompt::message('注册资金填写异常','goback');
	if($com_type == '-') $com_type = '';
	if($com_size == '-') $com_size = '';
	if($id)              $did = $id;

	//验证
	if(empty($cates))           Prompt::message('类别填写异常！','goback');
	if(empty($com_areaid))      Prompt::message('地区填写异常！','goback');
	if(!is_numeric($com_size))  Prompt::message('公司规模填写异常！','goback');
	if($com_money<0)            Prompt::message('注册资金填写异常！','goback');
	if(is_int($com_year) && strlen($com_year)<4)	Prompt::message('注册年份填写异常！','goback');
	if(!empty($com_telephone)){
		if(!is_mobile($com_telephone)) Prompt::message('电话号码填写异常！','goback');
	}

	//主营范围
	$com_business = CompanyEntry::getBusiness($cates);


	//词条
	$sql = Sql::update(Table::name(Table::NAME_MERCHANT_DOC), array(
		'summary'         => $summary,
		'company_thumb'   => $com_thumb,
		'company_type'    => $com_type,
		'company_size'    => $com_size,
		'company_year'    => $com_year,
		'company_unit'    => $com_unit,
		'company_money'   => $com_money,
		'company_areaid'  => $com_areaid,
		'company_address' => $com_address,
		'company_contact' => $com_contact,
		'company_telephone'=> $com_telephone,
		'company_mail'    => $com_mail,
		'company_business'=> $com_business,

		'reason'          => $reason,
		'author'          => $_username,
		'authorid'        => $_userid,
		'time'            => time(),

	), 'did='.$did);

	if (!$db->query($sql)) {
		echo $db->error();exit();
	}

	//版本
	$sql = Sql::update(Table::name(Table::NAME_MERCHANT_EDITION), array(
		'content'         => $content,
		'summary'         => $summary,
		'company_thumb'   => $com_thumb,
		'company_type'    => $com_type,
		'company_size'    => $com_size,
		'company_year'    => $com_year,
		'company_unit'    => $com_unit,
		'company_money'   => $com_money,
		'company_areaid'  => $com_areaid,
		'company_address' => $com_address,
		'company_contact' => $com_contact,
		'company_telephone'=> $com_telephone,
		'company_mail'    => $com_mail,
		'company_business'=> $com_business,

		'reason'          => $reason,
		'author'          => $_username,
		'authorid'        => $_userid,
		'time'            => time(),
	), 'eid='.$eid);


	if (!$db->query($sql)) {
		echo $db->error();exit();
	}


	//删除旧数据
	CompanyEntry::deleteCategory($did,$eid);

	//添加类别
	CompanyEntry::addCategory($cates,$did,$eid,$_username,$_userid);

	//删除草稿
	CompanyEntry::deleteDraft($did,$_userid);

	Prompt::message('编辑成功！','goback');


/* 编辑页面 */
} else {

	global $db;

	//企业存在未审核版本
	if(CompanyAdmin::getEditionAudit($id) && $_groupid!=1)	Prompt::message('该企业存在未审核版本，禁止编辑！','goback');



	$MOD_MEMBER =PCache::read('module-2.php');
	$modes  = explode('|', $MOD_MEMBER['com_mode']);         // 经营模式
    $types  = explode('|', $MOD_MEMBER['com_type']);         //公司类型
	$sizes  = explode('|', $MOD_MEMBER['com_size']);         // 公司规模
	$moneys = explode('|', $MOD_MEMBER['money_unit']);       // 资金类型

	//类别
	if(!$catid = $category) $catid = CompanyEntry::getCatId($id);

	//根据ID 分解数据库中的数据
	if (isset($id) && $id != '') {
		$companyInfo = CompanyEntry::getItemAndEdition($id);

		//分拆数据库内容
		extract($companyInfo);


		//获取类别数组
		$cates = $categoryArr = explode(',',$catid);

		//编辑器 选择的参数
		$parentid = CategoryModule::getParentid($categoryArr[0],PCache::read('category-25.php'));
		if($parentid==3834){
			$defaultDoc = array('发展历程','资本运作','运营体系','企业文化','产品品质','产品种类','配套供应','出口情况','品牌形象','发展战略','战略合作','研发实力','荣誉资质','质量认证','新闻事件',);
		}elseif($parentid==3835){
			$defaultDoc = array('发展历程','资本运作','运营体系','企业文化','商品品质','商品种类','配套供应','出口情况','质量认证','荣誉资质','发展战略','战略合作','新闻事件',	);
		}elseif($parentid==3836){
			$defaultDoc = array('发展历程','资本运作','运营体系','企业文化','综合实力','服务项目','质量认证','荣誉资质','发展战略','战略合作','物流线路','师资力量','新闻事件',	);
		}


		//返回 数组下标
		function getArrKey($arr,$parameter) {
			foreach($arr as $k => $v) {
				if($v==$parameter)	return $k;
			}
		}

		//版本库不存在该公司 时
		if(CompanyEntry::decideMerchantName($title,'edition')) {

			//判断企业表 有无此公司
			if($company = CompanyEntry::getCompanyInfo($title)){
				if($company['regunit'])       $company_unit  = getArrKey($moneys,$company['regunit']);//资金类型
				if($company['type'])          $company_type  = getArrKey($types,$company['type']);//公司类型
				if($company['size'])          $company_size  = getArrKey($sizes,$company['size']);//公司规模
				if($company['introduce'])     $summary = $company['introduce'];//公司简介
			}
		}

		//判断提交按钮类型
		$submitType = CompanyEntry::getSubmitType($id);

		include template('edit', 'company');
	}


}

